import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_mode/utils.dart';

/// Created by wangk on 2019/1/17.
class RefreshListDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var items = {
      'ListView': () => pushMaterial(context, CommonRefreshListDemo()),
      'CustomScrollView': () => pushMaterial(context, CustomScrollViewDemo()),
    };
    
    return Scaffold(
      appBar: AppBar(
        title: Text('RefreshDemo'),
        centerTitle: true,
      ),
      body: ListView.separated(
        itemBuilder: (context, position) {
          var item = items.entries.elementAt(position);
          return ListTile(
            title: Text(item.key),
            onTap: item.value,
          );
        },
        itemCount: items.length,
        separatorBuilder: (context, position) => Divider(
          height: 1,
        ),
      ),
    );
  }
}

class CommonRefreshListDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CommonRefreshListDemoState();
  }
}

class _CommonRefreshListDemoState extends State<CommonRefreshListDemo> {
//  GlobalKey<EasyRefreshState> _easyRefreshKey =
//      new GlobalKey<EasyRefreshState>();

  var _itemCount = 20;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('RefreshDemo'),
        centerTitle: true,
      ),
      body: EasyRefresh(
//        key: _easyRefreshKey,
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2));
          _itemCount = 20;
          setState(() {});
        },
          onLoad: () async {
          await Future.delayed(Duration(seconds: 2));
          if(_itemCount < 25) {
            _itemCount += 5;
            setState(() {});
          }
        },
        child: ListView.builder(
            itemBuilder: (context, position) {
              return ListTile(
                title: Text('position $position'),
              );
            },
            itemCount: _itemCount,
          )
      ),
    );
  }
}

class CustomScrollViewDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CustomScrollViewDemoState();
  }
}
class _CustomScrollViewDemoState extends State<CustomScrollViewDemo> {
//  GlobalKey<EasyRefreshState> _easyRefreshKey =
//  new GlobalKey<EasyRefreshState>();

  var _itemCount = 15;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: EasyRefresh(
//        key: _easyRefreshKey,
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2));
          _itemCount = 15;
          setState(() {});
        },
        onLoad: () async {
          await Future.delayed(Duration(seconds: 2));
          if(_itemCount < 25) {
            _itemCount += 5;
            setState(() {});
          }
        },
        child: CustomScrollView(
          slivers: <Widget>[
            const SliverAppBar(
              pinned: true,
              expandedHeight: 250.0,
              flexibleSpace: FlexibleSpaceBar(
                title: Text('RefreshDemo'),
                centerTitle: true,
              ),
            ),
            SliverList(
                delegate: SliverChildBuilderDelegate((context, which) {
                  return ListTile(
                    title: Text('position $which'),
                  );
                }, childCount: _itemCount))
          ],
        ),
      ),
    );
  }
}